﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Configuration;
using Newtonsoft.Json;
using System.Web.Script.Services;
using System.Data;

namespace PromotionEngine
{
    public partial class HierarchyHeader : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.Hierarchy> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.Hierarchy> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetHierarchyList()
        {
            List<Model.Hierarchy> lstSalesPerson = new List<Model.Hierarchy>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllHierarchy(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.Hierarchy salesPerson = new Model.Hierarchy();
                    salesPerson.CompanyCode = r["CompanyCode"].ToString();
                    salesPerson.CompanyName = r["CompanyName"].ToString();
                    salesPerson.HierarchyID = r["HierarchyID"].ToString();
                    salesPerson.HierarchyName = r["HierarchyName"].ToString();
                    salesPerson.HierarchyType = r["HierarchyType"].ToString();
                    salesPerson.ReferenceName = r["ReferenceName"].ToString();
                    salesPerson.HierarchyCode = r["HierarchyCode"].ToString();
                    salesPerson.CreatedUserID = r["CreatedUserID"].ToString();
                    lstSalesPerson.Add(salesPerson);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstSalesPerson };
                return new Response(lstSalesPerson, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateHierarchy(string oHierarchy, bool isInsert)
        {
            Model.Hierarchy hierarchy = JsonConvert.DeserializeObject<Model.Hierarchy>(oHierarchy);
            if (isInsert)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        hierarchy.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                        hierarchy.CompanyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonHiererchy(hierarchy.HierarchyID, hierarchy.HierarchyCode, hierarchy.HierarchyName, hierarchy.CompanyCode, hierarchy.CreatedUserID,
                hierarchy.HierarchyType, ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteHierarchy(string hierarchyID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteHierarchy(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), hierarchyID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetAllReference()
        {
            List<Model.ReferenceTypeValue> lstReferenceValue = new List<Model.ReferenceTypeValue>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReferenceValue(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), "HierarchyType");
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);

            }
            return string.Empty;
        }
    }
}